package cn.com.blueInfo.bpm.template.repository.impl;

import cn.com.blueInfo.bpm.common.entity.BaseEntity;
import cn.com.blueInfo.bpm.common.enums.BaseDeleted;
import cn.com.blueInfo.bpm.common.repository.BaseRepository;
import cn.com.blueInfo.bpm.template.entity.ParallelBranch;
import cn.com.blueInfo.bpm.template.mapper.ParallelBranchMapper;
import cn.com.blueInfo.bpm.template.repository.ParallelBranchRepository;
import cn.com.blueInfo.bpm.template.request.ParallelBranchDTO;
import cn.com.blueInfo.core.entity.CurrentLoginUserInfo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 并行分支表 数据服务实现类
 * @author suxch
 * @since 2025-09-01 19:30:44
 */
@Log4j2
@Service
public class ParallelBranchRepositoryImpl
        extends ServiceImpl<ParallelBranchMapper, ParallelBranch>
        implements ParallelBranchRepository, BaseRepository<ParallelBranch, ParallelBranchDTO.QueryDTO> {

    @Autowired
    private CurrentLoginUserInfo currentLoginUserInfo;

    /**
     * 提供获取 CurrentLoginUserInfo 的方法（由实现类提供，避免接口中注入）
     * @return CurrentLoginUserInfo
     */
    @Override
    public CurrentLoginUserInfo getCurrentLoginUserInfo() {
        return currentLoginUserInfo;
    }

    /**
     * 新增并行分支
     * @param parallelBranch 并行分支参数
     */
    @Override
    public Integer createParallelBranch(ParallelBranch parallelBranch) {
        return sqlExecuteResult(save(parallelBranch));
    }

    /**
     * 批量新增并行分支
     * @param parallelBranchList 并行分支参数列表
     */
    @Override
    public Integer batchCreateParallelBranch(List<ParallelBranch> parallelBranchList) {
        return sqlExecuteResult(saveBatch(parallelBranchList), parallelBranchList.size());
    }

    /**
     * 删除并行分支
     * @param parallelBranchId 并行分支ID
     */
    @Override
    public Integer deleteParallelBranch(Long parallelBranchId) {
        ParallelBranch parallelBranch = new ParallelBranch();
        parallelBranch.setId(parallelBranchId);
        parallelBranch.setDeleted(BaseDeleted.DELETED);
        return sqlExecuteResult(removeById(parallelBranch));
    }

    /**
     * 批量删除并行分支
     * @param parallelBranchIdList 并行分支ID列表
     */
    @Override
    public Integer batchDeleteParallelBranch(List<Long> parallelBranchIdList) {
        List<ParallelBranch> parallelBranchList = new ArrayList<>();
        for (Long parallelBranchId : parallelBranchIdList) {
            ParallelBranch parallelBranch = new ParallelBranch();
            parallelBranch.setId(parallelBranchId);
            parallelBranch.setDeleted(BaseDeleted.DELETED);
            parallelBranchList.add(parallelBranch);
        }
        return sqlExecuteResult(removeBatchByIds(parallelBranchList), parallelBranchList.size());
    }

    /**
     * 更新部分并行分支
     * @param parallelBranch 并行分支参数
     */
    @Override
    public Integer updateParallelBranchPartial(ParallelBranch parallelBranch) {
        validateUpdateId(parallelBranch.getId());
        LambdaUpdateWrapper<ParallelBranch> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(ParallelBranch::getId, parallelBranch.getId());
        return sqlExecuteResult(update(updateWrapper));
    }

    /**
     * 更新全部并行分支
     * @param parallelBranch 并行分支参数
     */
    @Override
    public Integer updateParallelBranch(ParallelBranch parallelBranch) {
        validateUpdateId(parallelBranch.getId());
        return sqlExecuteResult(updateById(parallelBranch));
    }

    /**
     * 查询一条并行分支
     * @param parallelBranchId 并行分支ID
     */
    @Override
    public ParallelBranch getParallelBranchById(Long parallelBranchId) {
        LambdaQueryWrapper<ParallelBranch> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ParallelBranch::getId, parallelBranchId);
        return getOne(queryWrapper);
    }

    /**
     * 分页查询并行分支
     * @param parallelBranchQueryDTO 并行分支参数
     */
    @Override
    public Page<ParallelBranch> getParallelBranchPage(ParallelBranchDTO.QueryDTO parallelBranchQueryDTO) {
        Page<ParallelBranch> parallelBranchPage = new Page<>(
                parallelBranchQueryDTO.getPage(), parallelBranchQueryDTO.getSize());
        LambdaQueryWrapper<ParallelBranch> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper(queryWrapper, parallelBranchQueryDTO);

        BaseDeleted deleted = parallelBranchQueryDTO.getDeleted();
        if (ObjectUtils.isNotEmpty(deleted) && deleted.equals(BaseDeleted.DELETED)) {
            return baseMapper.selectDeletePage(parallelBranchPage, queryWrapper);
        } else {
            return page(parallelBranchPage, queryWrapper);
        }
    }

    /**
     * 根据条件查询 并行分支 数量
     * @param parallelBranchQueryDTO 并行分支参数
     */
    @Override
    public Long getParallelBranchCount(ParallelBranchDTO.QueryDTO parallelBranchQueryDTO) {
        LambdaQueryWrapper<ParallelBranch> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper(queryWrapper, parallelBranchQueryDTO);

        BaseDeleted deleted = parallelBranchQueryDTO.getDeleted();
        if (ObjectUtils.isNotEmpty(deleted) && deleted.equals(BaseDeleted.DELETED)) {
            return baseMapper.selectDeleteCount(queryWrapper);
        } else {
            return count(queryWrapper);
        }
    }

    /**
     * 根据条件查询 并行分支
     * @param parallelBranchQueryDTO 并行分支参数
     */
    @Override
    public List<ParallelBranch> getParallelBranchList(ParallelBranchDTO.QueryDTO parallelBranchQueryDTO) {
        LambdaQueryWrapper<ParallelBranch> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper(queryWrapper, parallelBranchQueryDTO);

        BaseDeleted deleted = parallelBranchQueryDTO.getDeleted();
        if (ObjectUtils.isNotEmpty(deleted) && deleted.equals(BaseDeleted.DELETED)) {
            return baseMapper.selectDeleteList(queryWrapper);
        } else {
            return list(queryWrapper);
        }
    }

    private void queryWrapper(LambdaQueryWrapper<ParallelBranch> queryWrapper,
                              ParallelBranchDTO.QueryDTO parallelBranchQueryDTO) {

        Long gatewayId = parallelBranchQueryDTO.getGatewayId();
        queryWrapper.eq(ObjectUtils.isNotEmpty(gatewayId), ParallelBranch::getGatewayId, gatewayId);

        String branchName = parallelBranchQueryDTO.getBranchName();
        queryWrapper.eq(StringUtils.isNotBlank(branchName), ParallelBranch::getBranchName, branchName);

        Long targetNodeId = parallelBranchQueryDTO.getTargetNodeId();
        queryWrapper.eq(ObjectUtils.isNotEmpty(targetNodeId), ParallelBranch::getTargetNodeId, targetNodeId);

        addQueryWrapper(queryWrapper, parallelBranchQueryDTO, getCommonColumn());

    }

    private Map<String, SFunction<ParallelBranch, ?>> getCommonColumn() {
        return BaseEntity.getCommonColumn(
                ParallelBranch::getOrderNumber,
                ParallelBranch::getTenantId,
                ParallelBranch::getApplicationId,
                ParallelBranch::getStatus,
                ParallelBranch::getDeleted,
                ParallelBranch::getDeleteUserId,
                ParallelBranch::getDeleteTime,
                ParallelBranch::getRemark,
                ParallelBranch::getCreateUserId,
                ParallelBranch::getCreateTime,
                ParallelBranch::getUpdateUserId,
                ParallelBranch::getUpdateTime,
                ParallelBranch::getExtend1,
                ParallelBranch::getExtend2,
                ParallelBranch::getExtend3);
    }

}
